package ru.gelin.android.bells;

import java.util.Calendar;

import ru.gelin.android.bells.timers.Timer;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

/**
 *  Activity to edit the timer.
 */
public class EditTimerActivity extends SaveTimerActivity implements Constants {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        Uri uri = intent.getData();
        Log.d(TAG, "editing " + uri);
        final Timer timer = timers.select(
                Integer.parseInt(uri.getSchemeSpecificPart()));
        if (savedInstanceState == null) {
            initialStart = timer.getStartTime();
        }
        loadTimer(timer);

        final Button nowButton = (Button)findViewById(R.id.timer_start_now);
        nowButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setStartNow();
            }
        });

        final Button submitButton = (Button)findViewById(R.id.submit_button);
        submitButton.setText(R.string.button_save);
        submitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                saveTimer(timer);
            }
        });
    }
    
    void loadTimer(Timer timer) {
        CheckBox enabled = (CheckBox)findViewById(R.id.timer_enabled);
        enabled.setChecked(timer.isEnabled());
        
        TimePicker start = (TimePicker)findViewById(R.id.timer_start);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timer.getStartTime());
        start.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        start.setCurrentMinute(calendar.get(Calendar.MINUTE));
        
        TimePicker period = (TimePicker)findViewById(R.id.timer_period);
        period.setCurrentHour(timer.getPeriod().getHours());
        period.setCurrentMinute(timer.getPeriod().getMinutes());
        
        TextView message = (TextView)findViewById(R.id.timer_message);
        message.setText(timer.getMessage());
        
        Spinner alarm = (Spinner)findViewById(R.id.timer_alarm);
        Uri ringtoneUri = Uri.parse(timer.getAlarm());
        int ringtonePosition = ringtoneManager.getRingtonePosition(ringtoneUri);
        if (ringtonePosition >= 0) {
            selectedRingtone = ringtonePosition;
            alarm.setSelection(ringtonePosition, false);
        }
    }
    
    void setStartNow() {
        Calendar now = Calendar.getInstance();
        TimePicker start = (TimePicker)findViewById(R.id.timer_start);
        start.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
        start.setCurrentMinute(now.get(Calendar.MINUTE));
    }
    
}
